<?php
YII::import('application.components.autobid.AutoBidDao');

class Frozen
{

    public $customer_id;
    public $ip;
    public $customerInfo;
    public $customerInfo_data;
    
    protected $_customerModel;
    protected $_customerInfoModel;

    public function customerModel()
    {
        if (! $this->_customerModel instanceof Customer) {
            $this->_customerModel = new Customer();
            $this->customerInfo = $this->_customerModel->findByPk($this->customer_id);
        }
        return $this->_customerModel;
    }
    public function getCustomerInfo(){
        $this->_customerInfoModel=new CustomerInfo();
        $this->customerInfo_data=$this->_customerInfoModel->find('customer_id='.$this->customer_id);
        return $this;        
    }
    public function frozee()
    {
        $this->customerModel()->updateAll(array(
            'status' => 2
        ), 'id = :customer_id', array(
            ':customer_id' => $this->customer_id
        ));
        $this->addLogoutMark();
        return $this;
    }

    public function unFrozee()
    {
       
        $this->getCustomerInfo()->updateAll(array(
            'need_check_login'=> '0'           
        ),
            'customer_id='.$this->customer_id
        );
        $this->customerModel()->updateAll(array(
            'status' => 1
        ), 'id = :customer_id', array(
            ':customer_id' => $this->customer_id
        ));
        
        $this->delLogoutMark();
        return $this;
    }

    public function addLogoutMark()
    {
        Yii::app()->dbRedis->set('LOGOUT_' . $this->customer_id, $this->customer_id);
    }

    public function delLogoutMark()
    {
        Yii::app()->dbRedis->delete('LOGOUT_' . $this->customer_id);
    }

    public function addMessage($messages, $type = null)
    {
        $frozenMessage = new FrozenLog();
        $frozenMessage->customer_id = $this->customer_id;
        $frozenMessage->customer_email = $this->customerInfo->email;
        $frozenMessage->message = $messages;
        $frozenMessage->type = $type;
        $frozenMessage->created=time();
        $frozenMessage->insert();
        return $this;
    }

    public function getLoginCustomer($type)
    {
        
        $login_log = new LoginLog();
        if ($type == 'account') {
            $login_log_info = $login_log->findAll('ip="' . $this->ip . '"' . ' and type!=3 and created > '.(time()-3600));
            
        } 
        if ($type == 'ip') {
            $login_log_info = $login_log->findAll('customer_id=' . $this->customer_id . ' and type!=3 and created > '.(time()-3600));
        }   
        
        
        
        return $login_log_info;
    }
    public function checkCustomer(){
        $list = array();
        $check_customer=$this->getLoginCustomer('account');
        
        if (!empty($check_customer)){
            foreach ($check_customer as $loginInfo){
                $className = __CLASS__;
                $frozenCustomer = new $className();
                if (!array_key_exists($loginInfo->customer_id, $list)){
                    if($loginInfo->customer_id!=$this->customer_id){                                                                  
                    $frozenCustomer->customer_id = $loginInfo->customer_id;
                    $list[] = $frozenCustomer;
                    }
                }
        
            }
        }
        return $list;
    }
    public function checkIp(){
        $list = array();
        $check_customer=$this->getLoginCustomer('ip');
        if (!empty($check_customer)){
            foreach ($check_customer as $loginInfo){
                $className = __CLASS__;
                $frozenCustomer = new $className();
                if (!array_key_exists($loginInfo->customer_id, $list)){
                    if($loginInfo->ip!=$this->ip){
                        $frozenCustomer->customer_id = $loginInfo->customer_id;
                        $list[] = $frozenCustomer;  
                    }                  
                }
    
            }
        }
        return $list;
    }
    public function checkAll(){
        $autobid=new AutoBidDao();
        $this->getCustomerInfo();
        $check_login=$this->customerInfo_data->need_check_login;       
        if($check_login==1){
            $check_customer=$this->checkCustomer();
            $check_ip=$this->checkIp();
            if(!empty($check_customer)){
                $this->frozee()->addMessage('Due to the replacement of the account within one hour, you have to be frozen',1);
                $autobid->customerId=$this->customer_id;
                $autobid->delAutoBidOfSomeone();
                foreach ($check_customer as $customer_account){
                    $this->customer_id=$customer_account->customer_id;
                    $autobid->customerId=$this->customer_id;
                    $autobid->delAutoBidOfSomeone();
                    $this->frozee()->addMessage('Due to the replacement of the account within one hour, you have to be frozen',1);
                }
                return false;
            }
            else if(!empty($check_ip)){
                //                 $this->frozee()->addMessage('Since the replacement of IP within one hour, you have to be frozen',1);
                $autobid->customerId=$this->customer_id;
                $autobid->delAutoBidOfSomeone();
                foreach ($check_ip as $customer_ip){
                    $this->customer_id=$customer_ip->customer_id;
                    $autobid->customerId=$this->customer_id;
                    $autobid->delAutoBidOfSomeone();
                    $this->frozee()->addMessage('Since the replacement of IP within one hour, you have to be frozen',1);
                }
            
                return false;
            
            }else{
                return true;
            }
        }else{
            return true;
        }
        
    }
}